{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(temperature=0, model=\"qwen2\", api_key=\"nn\", base_url='http://***/v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.invoke('去年中秋节是哪天？').content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "functions = [{\n",
    "        'name': 'get_item_info',\n",
    "        'description': '获取时间范围内某个产品的信息',\n",
    "        'parameters': {\n",
    "            'type': 'object',\n",
    "            'properties': {\n",
    "                'item': {\n",
    "                    'type': 'string',\n",
    "                    'description':\n",
    "                    '产品名称',\n",
    "                },\n",
    "                'start': {\n",
    "                    'type': 'string',\n",
    "                    'description':\n",
    "                    '时间范围的起始时间'\n",
    "                },\n",
    "                'end': {\n",
    "                    'type': 'string',\n",
    "                    'description':\n",
    "                    '时间范围的结束时间'\n",
    "                },\n",
    "            },\n",
    "            'required': ['event', 'start', 'end'],\n",
    "        }\n",
    "    }]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = llm.bind_tools(functions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.invoke('2023年1月12至2月1日笔记本的销量')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm.invoke('去年九月份笔记本的销量')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jionlp\n",
    "query = '去年中秋节的营收多少'\n",
    "def date_extract(query):\n",
    "    date = jionlp.parse_time(query)\n",
    "    return date['time']\n",
    "date = date_extract(query)\n",
    "print(date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_modify_prompt = '''\n",
    "你是一个日期转换助手，请按照如下格式对用户输入进行转化。\n",
    "\n",
    "用户输入：\n",
    "2023年端午节的订单销量是多少\n",
    "\n",
    "日期：\n",
    "['2023-06-22 00:00:00', '2023-06-22 23:59:59']\n",
    "\n",
    "输出：\n",
    "2023-06-22 00:00:00到2023-06-22 23:59:59的订单销量是多少\n",
    "\n",
    "用户输入：\n",
    "2022年中秋节的营收多少\n",
    "\n",
    "日期：\n",
    "['2022-09-10 00:00:00', '2022-09-10 23:59:59']\n",
    "\n",
    "输出：\n",
    "2022-09-10 00:00:00到2022-09-10 23:59:59营收多少\n",
    "\n",
    "用户输入：\n",
    "{}\n",
    "\n",
    "日期：\n",
    "{}\n",
    "\n",
    "输出：\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm = ChatOpenAI(temperature=0, model=\"qwen2\", api_key=\"nn\", base_url='http://10.250.2.23:8600/v1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = '去年第一季度的销量怎么样'\n",
    "modify_query = llm.invoke(date_modify_prompt.format(query, date_extract(query))).content\n",
    "print(modify_query)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "undefined.undefined.undefined"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
